﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PTTK_HTTT_06.BUS;

namespace PTTK_HTTT_06.GUI.DatHangNguyenLieu
{
    public partial class MHDatHangNguyenLieu : UserControl
    {
        int i = 0;
        public MHDatHangNguyenLieu()
        {
            InitializeComponent();
        }

        private void MHDatHangNguyenLieu_Load(object sender, EventArgs e)
        {
            cmbNguyenLieu.ValueMember = "MaNguyenLieu";
            cmbNguyenLieu.DisplayMember = "TenNguyenLieu";
            cmbNguyenLieu.DataSource = NGUYEN_LIEU.LayDSTenNguyenLieu();
            cmbNguyenLieu.SelectedItem = cmbNguyenLieu.Items.Count - 1;
        }

        private void cmbNguyenLieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            int _maNguyenLieu = Convert.ToInt32(cmbNguyenLieu.SelectedValue);            
            dgvNhaCungCap.DataSource = NHA_CUNG_CAP.LayThongTinTheoNguyenLieu(_maNguyenLieu);
        }

        private void txtThem_Click(object sender, EventArgs e)
        {
            i = dgvDonHang.RowCount;
            i--;
            if (numericUpDown1.Value != 0)
            {
                if (HopLe(dgvDonHang, cmbNguyenLieu.Text.ToString(), i) == 1)
                {
                    DataGridViewRow _row = new DataGridViewRow();
                    dgvDonHang.Rows.Add(_row);
                    dgvDonHang.Rows[i].Cells[0].Value = cmbNguyenLieu.Text.ToString();
                    dgvDonHang.Rows[i].Cells[1].Value = numericUpDown1.Value.ToString();
                    dgvDonHang.Rows[i].Cells[2].Value = dgvNhaCungCap.SelectedCells[0].Value.ToString();
                    dgvDonHang.Rows[i].Cells[4].Value = cmbNguyenLieu.SelectedValue.ToString();
                    dgvDonHang.Rows[i].Cells[5].Value = dgvNhaCungCap.SelectedCells[3].Value.ToString();
                    i++;
                }
                else
                {
                    MessageBox.Show("Nguyên liệu đã thêm bên dưới !");
                }
            }
            else
            {
                MessageBox.Show("Bạn đã nhập thiếu thông tin !");                
            }
        }

        private int HopLe(DataGridView data, string str, int count)
        {
            int i = 0;
            while (i < count)
            {
                if (data.Rows[i].Cells[0].Value.ToString() == str)
                    return 0;
                i++;
            }
            return 1;
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            DON_DAT_HANG _dh = new DON_DAT_HANG();
            _dh.MaNhanVien = 7;
            _dh.NgayLap = DateTime.Today;            
            _dh.NgayGiao = dateTimePicker1.Value;
            _dh.TinhTrang = 0;
            if(DateTime.Compare(_dh.NgayGiao,_dh.NgayLap) < 0)
            {
                MessageBox.Show("Ngày giao phải sau ngày lập đơn hàng : " + DateTime.Today.ToString());
                return;
            }
            for (int i = 0; i < dgvDonHang.Rows.Count - 1; i++)            
            {
                if (dgvDonHang.Rows[i].Cells[3].Value == null || dgvDonHang.Rows[i].Cells[3].Value == DBNull.Value || String.IsNullOrEmpty(dgvDonHang.Rows[i].Cells[3].Value.ToString()))
                {
                    MessageBox.Show("Đơn giá không được để trống !");
                    return;
                }              
            }
            double sum = 0;
            for (int i = 0; i < dgvDonHang.Rows.Count - 1; ++i)
            {
                double temp = Convert.ToInt32(dgvDonHang.Rows[i].Cells[1].Value) * Convert.ToInt32(dgvDonHang.Rows[i].Cells[3].Value);
                sum += temp;
            }
            _dh.TongTien = sum;
            DON_DAT_HANG.ThemDonDatHang(_dh);
            _dh.MaDonDatHang = DON_DAT_HANG.LayMaDonDatHang();
            CT_DON_DAT_HANG _ctdh = new CT_DON_DAT_HANG();
            _ctdh.MaDonDatHang = _dh.MaDonDatHang;

            DialogResult _dialog = MessageBox.Show("Bạn có muốn tạo đơn hàng: \n Ngày giao: " + _dh.NgayGiao + "\n Tổng tiền: " + _dh.TongTien + " vnd", "XÁC NHẬN", MessageBoxButtons.OKCancel);
            if (_dialog == DialogResult.OK)
            {
                if (dgvDonHang.Rows.Count > 1)
                {
                    //NHA_CUNG_CAP _nguyenLieu = new NHA_CUNG_CAP();
                    int i = 0;
                    while (dgvDonHang.Rows.Count > 1)
                    {
                        if (dgvDonHang.Rows[i].Cells[0].Value == null)
                        {
                            dgvDonHang.Rows.RemoveAt(i);
                        }
                        else
                        {
                            _ctdh.MaNguyenLieu = int.Parse(dgvDonHang.Rows[i].Cells[4].Value.ToString());
                            _ctdh.MaNhaCungCap = int.Parse(dgvDonHang.Rows[i].Cells[5].Value.ToString());
                            _ctdh.SoLuong = int.Parse(dgvDonHang.Rows[i].Cells[1].Value.ToString());
                            _ctdh.GiaNhap = double.Parse(dgvDonHang.Rows[i].Cells[3].Value.ToString());
                            CT_DON_DAT_HANG.ThemCTDonDatHang(_ctdh);
                            dgvDonHang.Rows.RemoveAt(i);
                        }
                    }
                    if (dgvDonHang.RowCount == 1)
                    {
                        MessageBox.Show("Thêm thành công đơn hàng \n Ngày giao: " + _dh.NgayGiao + "\n Tổng tiền: " + _dh.TongTien + " vnd");
                    }
                }
            }
        }

        #region Ko cho nhap chu dgvDonHang column DonGia
        private void dgvDonHang_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            e.Control.KeyPress -= new KeyPressEventHandler(itemID_KeyPress);//This line of code resolved my issue
            if (dgvDonHang.CurrentCell.ColumnIndex == dgvDonHang.Columns["DonGia"].Index)
            {
                TextBox itemID = e.Control as TextBox;
                if (itemID != null)
                {
                    itemID.KeyPress += new KeyPressEventHandler(itemID_KeyPress);
                }
            }
        }

        private void itemID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }
        #endregion
        
    }
}
